package com.tianyi.controller.wechat;

import com.github.pagehelper.PageInfo;
import com.tianyi.comUser.ComUserService;
import com.tianyi.controller.BaseController;
import com.tianyi.controller.comUser.ComUserController;
import com.tianyi.dao.entity.orderProducts.Orderproducts;
import com.tianyi.dao.entity.orders.Orders;
import com.tianyi.dao.entity.productInfo.ProductInfo;
import com.tianyi.lib.model.WebResult;
import com.tianyi.lib.req.AddProductInfosForOrderReq;
import com.tianyi.lib.req.BaseReq;
import com.tianyi.lib.req.OrderToConfirmReq;
import com.tianyi.lib.req.QueryPage;
import com.tianyi.model.SimpleLoginUser;
import com.tianyi.vo.comUser.ComUserVO;
import com.tianyi.wechat.WechatOrdersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;

/**
 * wechat controller
 *
 * @author oh_grd
 * @date 2020/09/01
 */
@RestController
@RequestMapping("order")
public class WeChatOrderController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(WeChatOrderController.class);

    @Autowired
    private ComUserService comUserService;

    @Autowired
    private WechatOrdersService wechatOrdersService;

    @RequestMapping(value = "addOrder", method = RequestMethod.POST)
    public WebResult<String> addOrder(@RequestBody OrderToConfirmReq req){//JSON可用

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.addOrder(req);
    }

    @RequestMapping(value = "deleteOrderById", method = RequestMethod.POST)
    public WebResult<String> deleteOrder(@RequestBody Long id){//JSON可用

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.deleteOrderById(id);
    }

    @RequestMapping(value = "updateOrderById", method = RequestMethod.POST)
    public WebResult<String> updateOrderById(@RequestBody Orders order){//JSON可用

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.updateOrderById(order);
    }

    @RequestMapping(value = "queryOrdersByPageByStatus", method = RequestMethod.POST)//JSON可用
    public WebResult<List<Orders>> queryOrdersByPageByStatus(@RequestBody QueryPage queryPage){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.queryOrdersByPageByStatus(queryPage);
    }

    @RequestMapping(value = "confirmOrder", method = RequestMethod.POST)
    public WebResult<String> confirmOrder(@RequestBody OrderToConfirmReq req){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.confirmOrder(req);
    }


    @RequestMapping(value = "commentOrder", method = RequestMethod.POST)
    public WebResult<String> commentOrder(@RequestBody OrderToConfirmReq req){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.commentOrderById(req);
    }

    @RequestMapping(value = "changeLendStatus", method = RequestMethod.POST)
    public WebResult<String> changeLendStatus(@RequestBody OrderToConfirmReq req){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.changeLendStatusById(req);
    }

    @RequestMapping(value = "changeOrderStatus", method = RequestMethod.POST)
    public WebResult<String> changeOrderStatus(@RequestBody OrderToConfirmReq req){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.changeOrderStatusById(req);
    }

    @RequestMapping(value = "queryAllOrders", method = RequestMethod.POST)
    public WebResult<List<Orders>> queryAllOrders(@RequestBody OrderToConfirmReq req){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.queryAllOrders(req);
    }

    @RequestMapping(value = "queryAllOrdersByStatus", method = RequestMethod.POST)
    public WebResult<List<Orders>> queryAllOrdersByStatus(@RequestBody OrderToConfirmReq req){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.queryAllOrdersByStatus(req);
    }

    @RequestMapping(value = "queryOrdersByPage", method = RequestMethod.POST)
    public WebResult<List<Orders>> queryOrdersByPage(@RequestBody QueryPage queryPage){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.queryOrdersByPage(queryPage);
    }

    @RequestMapping(value = "queryOrdersByStatusPageInfo", method = RequestMethod.POST)
    public WebResult<?> queryOrdersByPageInfo(@RequestBody QueryPage queryPage){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        WebResult<PageInfo<Orders>> pageInfoWebResult = wechatOrdersService.queryOrdersByStatusPageInfo(queryPage);
        return pageInfoWebResult;
    }

    //@RequestMapping(value = "queryProductsInfoByOrderNumber", method = RequestMethod.POST)
    //public WebResult<List<ProductInfo>> queryProductsInfoByOrderNumber(@RequestBody Long req){
    //    return wechatOrdersService.queryProductsInfoByOrderNumber(req);
    //}

    @RequestMapping(value = "addProductInfosForOneOrder", method = RequestMethod.POST)
    public WebResult<String> addProductInfosForOneOrder(@RequestBody AddProductInfosForOrderReq req){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.addProductInfosForOneOrder(req);
    }

    @RequestMapping(value = "queryProductInfosForOneOrder", method = RequestMethod.POST)
    public WebResult<List<Orderproducts>> queryProductInfosForOneOrder(@RequestBody long orderNumber){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.queryProductInfosForOneOrder(orderNumber);
    }

    @RequestMapping(value = "makeProductInfosPDFForOneOrder", method = RequestMethod.POST)
    public WebResult<String> makeProductInfosPDFForOneOrder(@RequestBody long orderNumber){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return wechatOrdersService.makeProductInfosPDFForOneOrder(orderNumber);
    }

}

